package cn.com.qc.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;

import cn.com.qc.R;
import cn.com.qc.adapter.TextAdapter;


public class ViewMiddle extends LinearLayout implements ViewBaseAction {
	
	private ListView regionListView;
	private ListView plateListView;
	private ArrayList<String> groups = new ArrayList<String>();
	private LinkedList<String> childrenItem = new LinkedList<String>();
	private SparseArray<LinkedList<String>> children = new SparseArray<LinkedList<String>>();
	private TextAdapter plateListViewAdapter;
	private TextAdapter earaListViewAdapter;
	private OnSelectListener mOnSelectListener;
	private int tEaraPosition = 0;
	private int tBlockPosition = 0;
	private String showString = "不限";
	private String city = "{citys:[{\"province\":\"广东\",\"sub\":[{\"city\":\"广州\"},{\"city\":\"深圳\"},{\"city\":\"东莞\"},{\"city\":\"潮州\"},{\"city\":\"中山\"},{\"city\":\"佛山\"},{\"city\":\"汕头\"}]},{\"province\":\"北京\",\"sub\":[{\"city\":\"广州\"},{\"city\":\"深圳\"},{\"city\":\"东莞\"},{\"city\":\"潮州\"},{\"city\":\"中山\"},{\"city\":\"佛山\"},{\"city\":\"汕头\"}]},{\"province\":\"上海\",\"sub\":[{\"city\":\"广州\"},{\"city\":\"深圳\"},{\"city\":\"东莞\"},{\"city\":\"潮州\"},{\"city\":\"中山\"},{\"city\":\"佛山\"},{\"city\":\"汕头\"}]}\n"+ "]}\n";
	public ViewMiddle(Context context) {
		super(context);
		init(context);
	}

	public ViewMiddle(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public void updateShowText(String showArea, String showBlock) {
		if (showArea == null || showBlock == null) {
			return;
		}
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).equals(showArea)) {
				earaListViewAdapter.setSelectedPosition(i);
				childrenItem.clear();
				if (i < children.size()) {
					childrenItem.addAll(children.get(i));
				}
				tEaraPosition = i;
				break;
			}
		}
		for (int j = 0; j < childrenItem.size(); j++) {
			if (childrenItem.get(j).replace("不限", "").equals(showBlock.trim())) {
				plateListViewAdapter.setSelectedPosition(j);
				tBlockPosition = j;
				break;
			}
		}
		setDefaultSelect();
	}

	private void init(Context context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_region, this, true);
		regionListView = (ListView) findViewById(R.id.listView);
		plateListView = (ListView) findViewById(R.id.listView2);
		setBackgroundDrawable(getResources().getDrawable(
				R.drawable.choosearea_bg_left));
		InputStream is = ViewMiddle.this.getClass().getClassLoader().getResourceAsStream("assets/" + "city_json.json");
		InputStreamReader streamReader = new InputStreamReader(is);
		BufferedReader reader = new BufferedReader(streamReader);
		String line;
		StringBuilder stringBuilder = new StringBuilder();
		try {
			while ((line = reader.readLine()) != null) {
				// stringBuilder.append(line);
				stringBuilder.append(line);
			}
			reader.close();
			reader.close();
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			JSONObject citys = new JSONObject(stringBuilder.toString());
			JSONArray citys_array = citys.getJSONArray("provinces");
			for(int i=0;i<citys_array.length();i++){
				JSONObject province_item = citys_array.getJSONObject(i);
				String province = province_item.getString("provinceName");
				groups.add(province);
				JSONArray citys_sub = province_item.getJSONArray("citys");
				LinkedList<String> tItem = new LinkedList<String>();
				for(int j=0;j<citys_sub.length();j++){
					JSONObject city_item = citys_sub.getJSONObject(j);
					String city = city_item.getString("citysName");
					tItem.add(city);
				}
				children.put(i, tItem);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		earaListViewAdapter = new TextAdapter(context, groups,
				R.drawable.choose_item_selected,
				R.drawable.choose_eara_item_selector);
		earaListViewAdapter.setTextSize(17);
		earaListViewAdapter.setSelectedPositionNoNotify(tEaraPosition);
		regionListView.setAdapter(earaListViewAdapter);
		earaListViewAdapter
				.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, int position) {
						if (position < children.size()) {
							childrenItem.clear();
							childrenItem.addAll(children.get(position));
							plateListViewAdapter.notifyDataSetChanged();
						}
					}
				});
		if (tEaraPosition < children.size())
			childrenItem.addAll(children.get(tEaraPosition));
		plateListViewAdapter = new TextAdapter(context, childrenItem,
				R.drawable.choose_item_right,
				R.drawable.choose_plate_item_selector);
		plateListViewAdapter.setTextSize(15);
		plateListViewAdapter.setSelectedPositionNoNotify(tBlockPosition);
		plateListView.setAdapter(plateListViewAdapter);
		plateListViewAdapter
				.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, final int position) {
						
						showString = childrenItem.get(position);
						if (mOnSelectListener != null) {
							
							mOnSelectListener.getValue(showString);
						}

					}
				});
		if (tBlockPosition < childrenItem.size())
			showString = childrenItem.get(tBlockPosition);
		if (showString.contains("不限")) {
			showString = showString.replace("不限", "");
		}
		setDefaultSelect();

	}

	public void setDefaultSelect() {
		regionListView.setSelection(tEaraPosition);
		plateListView.setSelection(tBlockPosition);
	}

	public String getShowText() {
		return showString;
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		public void getValue(String showText);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}
}
